package leetcode.editor.cn.q1_300.q250;
//将非负整数 num 转换为其对应的英文表示。 
//
// 
//
// 示例 1： 
//
// 
//输入：num = 123
//输出："One Hundred Twenty Three"
// 
//
// 示例 2： 
//
// 
//输入：num = 12345
//输出："Twelve Thousand Three Hundred Forty Five"
// 
//
// 示例 3： 
//
// 
//输入：num = 1234567
//输出："One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
// 
//
// 
//
// 提示： 
//
// 
// 0 <= num <= 2³¹ - 1 
// 
//
// 👍 316 👎 0

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Q273_IntegerToEnglishWords {
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        String[] lessThan20 = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
        String[] WholeTen = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

        String bai = "Hundred";
        String qian = "Thousand";
        String baiWan = "Million";
        String shiyi = "Billion";

        public String numberToWords(int num) {
            if (num < 20) {
                return lessThan20[num];
            }
            /*
             英文每到 3 个 0 分段 千/百万/十亿
             其他的都按照百十个位读取，0-19单独 20-99为十位+个位，百位为个位+Hundred
             */
            List<String> strList = new ArrayList<>();
            if (num > 999999999) {
                strList.addAll(lessThanK(num / 1000000000));
                strList.add(shiyi);
            }
            num = num % 1000000000;
            if (num > 999999) {
                strList.addAll(lessThanK(num / 1000000));
                strList.add(baiWan);
            }
            num = num % 1000000;
            if (num > 999) {
                strList.addAll(lessThanK(num / 1000));
                strList.add(qian);
            }

            strList.addAll(lessThanK(num % 1000));

            return strList.stream().collect(Collectors.joining(" "));
        }

        private List<String> lessThanK(int num) {
            int bak = num;
            List<String> strList = new ArrayList<>();
            if (num > 99) {
                strList.add(lessThan20[num / 100]);
                strList.add(bai);
            }
            num = num % 100;
            if (num >= 20) {
                strList.add(WholeTen[num / 10 - 2]);
                if (num % 10 != 0) {
                    strList.add(lessThan20[num % 10]);
                }
            } else {
                if (bak != 0 && num != 0) {
                    strList.add(lessThan20[num]);
                }
            }
            return strList;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q273_IntegerToEnglishWords().new Solution();
        // TO TEST
        System.out.println(solution.numberToWords(0));
        System.out.println(solution.numberToWords(20));
        System.out.println(solution.numberToWords(200));
        System.out.println(solution.numberToWords(2000));
        System.out.println(solution.numberToWords(20000));
        System.out.println(solution.numberToWords(200000000));
        // One Hundred Twenty Three
        System.out.println(solution.numberToWords(123));
        // Twelve Thousand Three Hundred Forty Five
        System.out.println(solution.numberToWords(12345));
        // One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
        System.out.println(solution.numberToWords(1234567));
        // Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven
        System.out.println(solution.numberToWords(Integer.MAX_VALUE));
    }
}